package com.guli.edu.controller.admin;

import com.guli.common.vo.R;
import com.guli.edu.form.VideoInfoForm;
import com.guli.edu.service.VideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(description="课时管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/admin/edu/video")
public class VideoAdminController {

	@Autowired
	private VideoService videoService;

	@ApiOperation(value = "新增课时")
	@PostMapping("save-video-info")
	public R save(
			@ApiParam(name = "videoForm", value = "课时对象", required = true)
			@RequestBody VideoInfoForm videoInfoForm){

		videoService.saveVideoInfo(videoInfoForm);
		return R.ok();
	}
	@ApiOperation(value = "根据ID查询课时")
	@GetMapping("video-info/{id}")
	public R getVideInfoById(
			@ApiParam(name = "id", value = "课时ID", required = true)
			@PathVariable String id){

		VideoInfoForm videoInfoForm = videoService.getVideoInfoFormById(id);
		return R.ok().data("item", videoInfoForm);
	}

	@ApiOperation(value = "更新课时")
	@PutMapping("update-video-info/{id}")
	public R updateCourseInfoById(
			@ApiParam(name = "VideoInfoForm", value = "课时基本信息", required = true)
			@RequestBody VideoInfoForm videoInfoForm,

			@ApiParam(name = "id", value = "课时ID", required = true)
			@PathVariable String id){

		videoService.updateVideoInfoById(videoInfoForm);
		return R.ok();
	}
	@ApiOperation(value = "根据ID删除课时")
	@DeleteMapping("{id}")
	public R removeById(
			@ApiParam(name = "id", value = "课时ID", required = true)
			@PathVariable String id){

		boolean result = videoService.removeVideoById(id);
		if(result){
			return R.ok();
		}else{
			return R.error().message("删除失败");
		}
	}

	@PostMapping("upload")
	public R uploadVideo(
			@ApiParam(name = "file", value = "文件", required = true)
			@RequestParam("file") MultipartFile file) throws Exception {

		String videoId = videoService.uploadVideo(file);
		return R.ok().message("视频上传成功").data("videoId", videoId);
	}
}